/*
 * pt2262.c
 *
 *  Created on: Mar 23, 2021
 *      Author: gary
 */

#include "pt2262.h"

uint16_t PWM_Buffer[32];

void PT2262_SendData(const uint8_t *dataBuffer)
{
	for(uint8_t i = 0; i < 12; i++)
	{
		if(dataBuffer[i] == 0)
		{
			PWM_Buffer[i * 2] = BIT_SHORT;
			PWM_Buffer[i * 2 + 1] = BIT_SHORT;
		}
		else if(dataBuffer[i] == 1)
		{
			PWM_Buffer[i * 2] = BIT_LONG;
			PWM_Buffer[i * 2 + 1] = BIT_LONG;
		}
		else
		{
			PWM_Buffer[i * 2] = BIT_SHORT;
			PWM_Buffer[i * 2 + 1] = BIT_LONG;
		}
	}
	PWM_Buffer[24] = BIT_SHORT;
	for(uint8_t i = 0; i < 7; i++)
	{
		PWM_Buffer[25 + i] = 0;
	}
	HAL_TIM_PWM_Start_DMA(&htim17, TIM_CHANNEL_1, (uint32_t*)PWM_Buffer, 32);
}

void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
{
	if(htim->Instance == TIM17)
	{
		HAL_TIM_PWM_Stop(&htim17, TIM_CHANNEL_1);
		HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_SET);
	}
}
